/*******************************************************************************
 * Package: com.song.boot.springstudy.thread.threadLocal
 * Type:    TestThreadLocal
 * Date:    2025-09-03 21:00
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.thread.threadLocal;

import com.song.boot.springstudy.entity.One;
import lombok.SneakyThrows;

/**
 * 功能描述： ThreadLocal 多线程操作共享的非线程安全成员变量
 *  执行结果会有问题
 *
 * @author Songxianyang
 * @date 2025-09-03 21:00
 */
@Deprecated
public class TestThreadLocal {
    // 将 “共享的全局对象” 存入ThreadLocal
    private static final One one = new One();
    private static final ThreadLocal<One> THREAD_LOCAL_ONE = new ThreadLocal<>();

    @SneakyThrows
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            one.setCount("1");
            THREAD_LOCAL_ONE.set(one);
            System.out.println(Thread.currentThread().getName());
            System.out.println(THREAD_LOCAL_ONE.get().getCount()+">>>>>>>"+Thread.currentThread().getName());
        });
        Thread thread1 = new Thread(() -> {
            one.setCount("2");
            THREAD_LOCAL_ONE.set(one);
            System.out.println(Thread.currentThread().getName());
            System.out.println(THREAD_LOCAL_ONE.get().getCount()+">>>>>>>"+Thread.currentThread().getName());
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
    }
}
